<?php
class db_functions_music {

	private $db;

	function __construct() {
		include_once 'db_connect.php';
		// connecting to database
		$this->db = new DB_Connect();
		$this->db->connect();
	}

	function __destruct() {

	}

	public function saveControl($id, $code_control, $type, $value, $device) {
		$table = DB_TABLE_control_to_desktop;
		if (strcmp($device, DEVICE_OTHER) == 0) {

			$table = DB_TABLE_control_desktop;

		}

		$result = mysql_query("INSERT INTO " . $table . "(`id`, `code_control`,`type`,`value`)VALUES ('$id','$code_control','$type','$value' )");

		if ($result) {
			return true;
		} else {

			// this row existed, start update playlist for id

			$result = mysql_query("UPDATE " . $table . " SET `code_control` = '$code_control', `type` = '$type', `value` = '$value' WHERE `id` = '$id'");

			if ($result) {

				return true;
			} else {
				return false;

			}

			e;

		}
	}
	
	public function resetCodeControl($id, $device){
		if (strcmp($device, DEVICE_OTHER) != 0) {

			$this->saveControl($id,0,1,1, $device);
		}
		
		
		
	}

	// get gcm_regID for desktop device
	public function getControl($id, $device) {
		$table = DB_TABLE_control_desktop;
		if (strcmp($device, DEVICE_OTHER) == 0) {

			$table = DB_TABLE_control_to_desktop;

		}
		

		$result = mysql_query("SELECT code_control,type,value FROM " . $table . " WHERE id = '$id' LIMIT 1");
		$ret = mysql_fetch_array($result);

		$re = array (
			'code_control' => $ret[0],
			'type' => $ret[1],
			'value' => $ret[2],

			
		);
		
		
		
		return $re;

	}

}
?>
